package cn.edu.hhu.DAO.impl;

import cn.edu.hhu.Bean.Admin;
import cn.edu.hhu.Bean.Student;
import cn.edu.hhu.DAO.Admin_infoDao;
import cn.edu.hhu.util.JDBCUtils;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @anthor: lddd
 * @date: 2020/5/16 - 21:00
 * @description:
 **/
public class Admin_infoDaoImpl implements Admin_infoDao {
    JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 添加一个管理员账户
     * @param admin
     * @return 是否添加成功
     */
    @Override
    public boolean insertAdmin(Admin admin) {
        try {
            String sql = "INSERT INTO admin_info VALUES(?,?)";
            int update = template.update(sql, admin.getAid(), admin.getPwd());
            if (update == 1)    return true;
            else return false;
        }catch (Exception e){
            return false;
        }
    }

    /**
     * 根据账号和密码查询用户
     * @param aid
     * @param pwd
     * @return 用户信息
     */
    @Override
    public Admin findAdminByIdAndPassword(String aid, String pwd) {
        try {
            String sql = "select * from admin_info where aid = ? and pwd = ?";
            Admin admin = template.queryForObject(sql, new BeanPropertyRowMapper<>(Admin.class), aid, pwd);
            return admin;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 删除一个管理员账户
     * @param admin
     * @return 是否成功
     */
    @Override
    public boolean deleteAdmin(Admin admin) {
        try {
            String sql = "DELETE FROM admin_info WHERE aid = ?";
            int update = template.update(sql, admin.getAid());
            if (update == 1)    return true;
            else return false;
        }catch (Exception e){
            return false;
        }
    }
}
